Archive and compressing files

Tar

In [2]:
mkdir lab
In [3]:
cd lab
In [20]:
cp /etc/default/* . 2> /dev/null

In [23]:
ls | head -5
alsa
avahi-daemon
bridge-utils
bsdmainutils
console-setup
In [44]:
du . -h # disk usage
176K	.
In [60]:
tar -c -f file.tar *
In [47]:
ls -lh file.tar
-rw-rw-r-- 1 milad milad 70K May  1 17:35 file.tar
In [48]:
ls * | grep -v tar | wc -l
41
In [61]:
shopt -s extglob # extended pattern matching syntax
rm !(file.tar)
In [62]:
ls
file.tar
In [64]:
tar -x -f file.tar
In [56]:
ls | head -5 | tr '\n' ' '
alsa avahi-daemon bridge-utils bsdmainutils console-setup 

gzip, gunzip, zcat

In [18]:
rm !(file.tar)
In [19]:
ls
file.tar
In [4]:
gzip -9 -k file.tar # -1 fast : -9 best
In [5]:
ls -lh
total 88K
-rw-rw-r-- 1 milad milad 70K May  1 17:45 file.tar
-rw-rw-r-- 1 milad milad 16K May  1 17:45 file.tar.gz
In [7]:
rm file.tar
In [8]:
ls
file.tar.gz
In [9]:
gzip --decompress file.tar.gz # -d, --uncompress, gunzip
In [25]:
ls -lh
total 72K
-rw-rw-r-- 1 milad milad 70K May  1 17:45 file.tar
In [20]:
tar -x -f file.tar
In [29]:
gzip -k -9 file.tar; ls
file.tar  file.tar.bz2  file.tar.gz
In [30]:
file file.tar.gz
file.tar.gz: gzip compressed data, was "file.tar", last modified: Mon May  1 13:41:06 2017, max compression, from Unix
In [31]:
cat file.tar.gz | gunzip > file.tar # get the input from stdin
In [32]:
gzip -d -c file.tar.gz > file2.tar #  Write output on standard output
In [ ]:
zcat file.tar.gz # similar to -> gunzip -c, gzip -d -c
In [35]:
ls -lhrt
total 312K
-rw-rw-r-- 1 milad milad  30K May  1 18:11 file.tar.gz
-rw-rw-r-- 1 milad milad 140K May  1 18:23 file.tar
-rw-rw-r-- 1 milad milad 140K May  1 18:23 file2.tar

bzip2, bunzip2, bzcat

In [40]:
rm !(file.tar) 2> /dev/null; ls
file.tar
In [41]:
bzip2 -9 file.tar
In [42]:
ls -lh
total 20K
-rw-rw-r-- 1 milad milad 17K May  1 18:23 file.tar.bz2
In [47]:
bunzip2 file.tar.bz2
In [48]:
ls
file.tar
In [49]:
bzip2 file.tar
In [50]:
ls -lh
total 20K
-rw-rw-r-- 1 milad milad 17K May  1 18:23 file.tar.bz2
In [54]:
bzcat file.tar.bz2 > new.tar
In [57]:
bzip2 -d -c file.tar.bz2 > another.tar # -c : Write the output on standard output
In [59]:
bunzip2 -c file.tar.bz2 > file.tar
In [62]:
cat file.tar.bz2 | bunzip2 > newfile.tar # cat file.tar.bz2 | bzip2 -d > newfile.tar
In [63]:
md5sum *.tar
2184bc65ce5e94608d6a342dc1949982  another.tar
2184bc65ce5e94608d6a342dc1949982  file.tar
2184bc65ce5e94608d6a342dc1949982  newfile.tar
2184bc65ce5e94608d6a342dc1949982  new.tar

xz, unxz, xzcat

In [69]:
rm !(file.tar)
In [70]:
xz -k file.tar
In [72]:
ls -lh
total 156K
-rw-rw-r-- 1 milad milad 140K May  1 18:30 file.tar
-rw-rw-r-- 1 milad milad  14K May  1 18:30 file.tar.xz

Back to tar

In [82]:
ls
file.tar
In [83]:
tar -xf file.tar
In [85]:
rm file.tar
In [86]:
ls | wc -l
41
In [87]:
du -h .
244K	.
In [88]:
mkdir cmp
In [89]:
tar -c -z -f cmp/my.tar.gz *
In [90]:
tar -cjf cmp/my.tar.bz2 *
In [91]:
tar -cJf cmp/my.tar.xz *
In [92]:
file cmp/*
cmp/my.tar.bz2: bzip2 compressed data, block size = 900k
cmp/my.tar.gz:  gzip compressed data, last modified: Mon May  1 14:23:16 2017, from Unix
cmp/my.tar.xz:  XZ compressed data
In [94]:
ls -lh cmp/*
-rw-rw-r-- 1 milad milad 49K May  1 18:53 cmp/my.tar.bz2
-rw-rw-r-- 1 milad milad 30K May  1 18:53 cmp/my.tar.gz
-rw-rw-r-- 1 milad milad 93K May  1 18:53 cmp/my.tar.xz
In [102]:
GZIP=-9 tar -cjf cmp/file.tar.gz * --exclude cmp
In [103]:
ls cmp/file.tar.gz -lh
-rw-rw-r-- 1 milad milad 17K May  1 19:08 cmp/file.tar.gz
In [ ]:
tar -xOf cmp/my.tar.gz > a.tar # extract to stdout
In [106]:
tar -t -f cmp/my.tar.gz | head -4 # list
alsa
avahi-daemon
bridge-utils
bsdmainutils
tar: write error

Paths in tar

In [128]:
tar -cf test.tar /etc/grub.d 2> /dev/null
In [129]:
tar -tf test.tar | tail -1
etc/grub.d/30_os-prober
In [133]:
tar -cPf test.tar /etc/grub.d 2> /dev/null # keep the leading /
In [137]:
tar -tf test.tar 2> /dev/null | tail -1
/etc/grub.d/30_os-prober
In [130]:
tar -cf test.tar -C /etc/grub.d . # Change dir
In [132]:
tar -tf test.tar | tail -1
./30_os-prober
In [ ]:
find /etc/grub.d/ | tar -cf grub.tar -T - -Pp # From stdin while keeping the / and permissions

Updating

In [169]:
rm -r *
In [185]:
echo 'hi 1' > 1 ; echo 'hi 2' > 2
In [186]:
tar -cf 1.tar 1; tar -cf 2.tar 2;
In [187]:
rm 1 2
In [188]:
ls -l
total 24
-rw-rw-r-- 1 milad milad 10240 May  1 19:39 1.tar
-rw-rw-r-- 1 milad milad 10240 May  1 19:39 2.tar
In [189]:
tar -xOf 1.tar # -O: to the stdout
hi 1
In [190]:
echo 'bye 1' > 1
In [191]:
tar -uf 1.tar 1  # update file 1 if it's newer
In [193]:
tar -tf 1.tar # list files
1
1
In [192]:
tar -xOf 1.tar
hi 1
bye 1
In [ ]:
tar -xf 2.tar --occurrence=1 filename
In [ ]:
tar -xf 2.tar --backup filename # 1 1~

Appending a tarball into archive

In [194]:
tar -A 1.tar -f 2.tar # Appends 1.tar into 2.tar
In [196]:
tar -f 2.tar -t # list files
2
1
1
In [197]:
tar -f 2.tar -xO
hi 2
hi 1
bye 1
In [199]:
rm 1 # remove the new one we just creat it
In [200]:
tar -f 2.tar -x 1 # only extract file with the name `1`
In [201]:
ls
1  1.tar  2.tar
In [202]:
cat 1
bye 1

Append a file into archive

In [29]:
tar -tf 1.tar
1
In [30]:
tar -r -f 1.tar 2
In [31]:
tar -tf 1.tar
1
2

Delete

In [203]:
tar -f 2.tar --delete 1
In [204]:
tar -tf 2.tar # list files
2
In [ ]:
tar -f 2.tar --delete --occurrence=2 filename

cpio

In [205]:
whatis cpio
cpio (1)             - copy files to and from archives
In [209]:
find /etc/grub.d | head -3
/etc/grub.d
/etc/grub.d/05_debian_theme
/etc/grub.d/40_custom
In [210]:
find /etc/grub.d | cpio -o > grub-d.cpio
106 blocks
In [211]:
file grub-d.cpio
grub-d.cpio: cpio archive
In [219]:
cpio -id --no-absolute-filenames < grub-d.cpio
cpio: Removing leading `/' from member names
106 blocks
In [220]:
ls
etc  grub-d.cpio
In [8]:
rm -r etc grub-d.cpio
In [2]:
ls /boot/init*
/boot/initrd.img-4.4.0-72-generic  /boot/initrd.img-4.4.0-75-generic
In [4]:
cp /boot/initrd.img-4.4.0-75-generic .
In [11]:
file initrd.img-4.4.0-75-generic
initrd.img-4.4.0-75-generic: gzip compressed data, last modified: Fri Apr 28 10:16:15 2017, from Unix
In [12]:
zcat initrd.img-4.4.0-75-generic | cpio -id --no-absolute-filenames
192678 blocks
In [14]:
rm initrd.img-4.4.0-75-generic
In [15]:
ls
bin  conf  etc  init  lib  lib64  run  sbin  scripts  usr  var

zip, unzip

The zip program puts one or more compressed files into a single zip archive, along with information about the files (name, path, date, time of last modifica‐ tion, protection, and check information to verify file integrity).

In [14]:
zip my.zip grub.tar
  adding: grub.tar (deflated 79%)
In [15]:
ls -lh
total 76K
-rw-rw-r-- 1 milad milad 60K May  2 12:13 grub.tar
-rw-rw-r-- 1 milad milad 13K May  2 12:14 my.zip
In [35]:
rm -r *
In [36]:
zip -jr9 grub.d.zip /etc/grub.d/  # junk the path
  adding: 05_debian_theme (deflated 58%)
  adding: 40_custom (deflated 29%)
  adding: 20_linux_xen (deflated 67%)
  adding: 00_header (deflated 68%)
  adding: 10_linux (deflated 67%)
  adding: README (deflated 35%)
  adding: 41_custom (deflated 46%)
  adding: 30_uefi-firmware (deflated 42%)
  adding: 30_os-prober (deflated 70%)
In [37]:
ls -lh
total 20K
-rw-rw-r-- 1 milad milad 20K May  2 12:54 grub.d.zip
In [42]:
file grub.d.zip
grub.d.zip: Zip archive data, at least v2.0 to extract

List files in a ZIP archive

In [47]:
unzip -l grub.d.zip
Archive:  grub.d.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     6258  2016-03-15 21:38   05_debian_theme
      214  2016-07-23 04:39   40_custom
    11082  2016-07-23 04:39   20_linux_xen
     9791  2016-07-23 04:39   00_header
    12512  2017-03-02 00:31   10_linux
      483  2016-07-23 04:39   README
      216  2016-07-23 04:39   41_custom
     1418  2016-07-23 04:39   30_uefi-firmware
    11692  2016-07-23 04:39   30_os-prober
---------                     -------
    53666                     9 files

the names, uncompressed file sizes and modification dates and times of the specified files are printed, along with totals for all files specified.

In [48]:
unzip -v grub.d.zip
Archive:  grub.d.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
    6258  Defl:X     2634  58% 2016-03-15 21:38 495dc52b  05_debian_theme
     214  Defl:X      151  29% 2016-07-23 04:39 7dc9d73d  40_custom
   11082  Defl:X     3654  67% 2016-07-23 04:39 800a6f67  20_linux_xen
    9791  Defl:X     3182  68% 2016-07-23 04:39 bc94e4f7  00_header
   12512  Defl:X     4071  68% 2017-03-02 00:31 6fd29927  10_linux
     483  Defl:X      314  35% 2016-07-23 04:39 84e28493  README
     216  Defl:X      116  46% 2016-07-23 04:39 0d9cbda7  41_custom
    1418  Defl:X      825  42% 2016-07-23 04:39 96573839  30_uefi-firmware
   11692  Defl:X     3505  70% 2016-07-23 04:39 3c1d6615  30_os-prober
--------          -------  ---                            -------
   53666            18452  66%                            9 files
In [56]:
zipinfo -2 grub.d.zip # read the man page, got a bunch of options to handle the list
etc/grub.d/
etc/grub.d/05_debian_theme
etc/grub.d/40_custom
etc/grub.d/20_linux_xen
etc/grub.d/00_header
etc/grub.d/10_linux
etc/grub.d/README
etc/grub.d/41_custom
etc/grub.d/30_uefi-firmware
etc/grub.d/30_os-prober

Zip Encryption

In [ ]:
zip -e my.zip /etc/ # encrypt using password
In [ ]:
zipcloak my.zip # after creation
In [ ]:
zipcloak -d my.zip # remove encryption

Comment

In [57]:
zip -jj9r w3m.zip /etc/w3m/
  adding: config (deflated 50%)
  adding: mailcap (deflated 5%)
In [58]:
unzip -v w3m.zip
Archive:  w3m.zip
 Length   Method    Size  Cmpr    Date    Time   CRC-32   Name
--------  ------  ------- ---- ---------- ----- --------  ----
    1262  Defl:X      625  51% 2017-03-02 00:16 6cde7dbe  config
      44  Defl:X       42   5% 2017-03-02 00:16 eac7cd80  mailcap
--------          -------  ---                            -------
    1306              667  49%                            2 files
In [64]:
zipnote w3m.zip
@ config
@ (comment above this line)
@ mailcap
@ (comment above this line)
@ (zip file comment below this line)
In [ ]:
nano tmp

@ config Comment by ravexina (Milad As) @ (comment above this line) @ mailcap github.com/ravexina/ @ (comment above this line) @ (zip file comment below this line)

In [66]:
zipnote -w w3m.zip < tmp
In [67]:
zipnote w3m.zip
@ config
Comment by ravexina (Milad As)
@ (comment above this line)
@ mailcap
github.com/ravexina/
@ (comment above this line)
@ (zip file comment below this line)
In [74]:
unzip -l w3m.zip
Archive:  w3m.zip
  Length      Date    Time    Name
---------  ---------- -----   ----
     1262  2017-03-02 00:16   config
Comment by ravexina (Milad As)
       44  2017-03-02 00:16   mailcap
github.com/ravexina/
---------                     -------
     1306                     2 files

Renaming

Save it in tmp

@ config @=newconfig-name Comment by ravexina (Milad As) @ (comment above this line) @ mailcap github.com/ravexina/ @ (comment above this line) @ (zip file comment below this line)

In [ ]:
zipnote -w w3m.zip < tmp
In [76]:
zipinfo -2 w3m.zip
newconfig-name
mailcap

Lecture notes

License

Creative Commons License

Linux Notes by Milad As (Ravexina) is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.


ravexina's gitlab

ravexina's github